package xyz.xtgl.erp.system.controller;

import common.core.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import xyz.xtgl.config.helper.MyCustomUserDetails;
import xyz.xtgl.erp.system.entity.SysUser;
import xyz.xtgl.erp.system.service.ISysUserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author liubo
 * @created 2020-02-03 13:12
 */
@Controller
public class MainController {

    @Autowired
    private ISysUserService iSysUserService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    /**
     * 登录页面跳转
     * @return 登录页面
     */
    @GetMapping("/login")
    public String login(HttpServletRequest reqeust, HttpServletResponse response) throws IOException {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if(authentication instanceof UsernamePasswordAuthenticationToken){
            response.sendRedirect("index");
            return null;
        }else{
            return "login";
        }
    }

    @GetMapping({"/index","","/"})
    public String index(HttpServletRequest reqeust, HttpServletResponse response) throws IOException {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if(authentication instanceof UsernamePasswordAuthenticationToken){
            SysUser user = ((MyCustomUserDetails) authentication.getPrincipal()).getData();
            reqeust.setAttribute("loginName",user.getName());
            return "index";
        }else{
            response.sendRedirect("login");
            return null;
        }
    }

    @GetMapping("/console")
    public String console(){
        return "main/console";
    }

    @GetMapping("/modifyPassword")
    public String modifyPassword(){
        return "main/password";
    }

    @ResponseBody
    @PostMapping("/modifyPassword")
    public Result modifyPassword(String oldPassword, String newPassword){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if(authentication instanceof UsernamePasswordAuthenticationToken){
            SysUser user = ((MyCustomUserDetails) authentication.getPrincipal()).getData();
            SysUser nowUser = this.iSysUserService.getById(user.getId());
            if(passwordEncoder.matches(oldPassword,nowUser.getPassword())){
                SysUser update=new SysUser();
                update.setId(nowUser.getId());
                update.setPassword(passwordEncoder.encode(newPassword));
                update.setVersion(nowUser.getVersion());
                this.iSysUserService.updateById(update);
                return Result.success("密码修改成功");
            }
            return Result.fail("您当前密码输入错误,请重新数数据");
        }
        return Result.fail("密码修改失败");
    }
}
